using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fieldbook.Business;
using Fieldbook.Entities;
using System.Collections.Generic;

namespace Fieldbook.Testwebsite.User
{
    public partial class Machines : FieldbookPage
    {
        private List<Measureunit> measureUnits;

        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            measureUnits = MeasureUnitController.LoadAllMeasureUnits();
        }

        public void LoadMachines()
        {
            if (dgvMachineCategories.SelectedIndex != -1)
            {
                long userID = (long)Session["userID"];
                long categoryID = Int64.Parse(dgvMachineCategories.SelectedDataKey.Value.ToString());
                List<Machine> items = MachineController.LoadMachines(userID, categoryID);
                dgvMachines.DataSource = items;
                dgvMachines.DataBind();
            }
        }
        protected void DgvMachineCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMachines();
        }

        protected void dgvMachines_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                Label lblPowerMU = (Label)e.Row.Cells[1].FindControl("lblPowerMU");
                if (!String.IsNullOrEmpty(lblPowerMU.Text))
                {
                    int powerMU = Int32.Parse(lblPowerMU.Text);
                    Measureunit unit = measureUnits.Find(delegate(Measureunit mu) { return (int)mu.Measureunitid == powerMU; });
                    lblPowerMU.Text = unit.Description;
                }
                Label lblVolumenMU = (Label)e.Row.Cells[2].FindControl("lblVolumenMU");
                if (!String.IsNullOrEmpty(lblVolumenMU.Text))
                {
                    int volumenMU = Int32.Parse(lblVolumenMU.Text);
                    Measureunit unit = measureUnits.Find(delegate(Measureunit mu) { return (int)mu.Measureunitid == volumenMU; });
                    lblVolumenMU.Text = unit.Description;
                }
            }
        }

        /// <summary>
        /// Add machine category
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddCategory_Command(object sender, CommandEventArgs e)
        {
            bool sucess = false;
            if (e.CommandArgument.ToString() == "global")
            {
                sucess = MachineController.InsertMachineCategory(txtMachineCategory.Text);
            }
            else
            {
                long userID = (long)Session["userID"];
                sucess = MachineController.InsertMachineCategory(txtMachineCategory.Text, userID);
            }
            if (sucess)
            {
                this.lblOperationStatus.Text = "Category sucessfully added";
                this.txtMachineCategory.Text = "";
                this.dgvMachineCategories.DataBind();
                this.ddlMachineCategory.DataBind();
            }
            else
            {
                this.lblOperationStatus.Text = "Error adding category";
            }
        }

        /// <summary>
        /// Add new machine
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddMachine_Command(object sender, CommandEventArgs e)
        {
            bool sucess = false;
            long userID = (long)Session["userID"];
            long? hHoldID = AppUserController.GetHoueHoldID(userID);
            Machine m = new Machine();
            m.CategoryID = Int32.Parse(this.ddlMachineCategory.SelectedValue);
            if (hHoldID != null) m.HouseHoldID = (long)hHoldID;
            if (!String.IsNullOrEmpty(this.txtRegistration.Text)) m.Registration = this.txtRegistration.Text;
            if (!String.IsNullOrEmpty(this.txtPower.Text))
            {
                m.Power = Decimal.Parse(this.txtPower.Text);
                m.PowerMU = Int32.Parse(this.ddlPowerMU.SelectedValue);
            }
            if (!String.IsNullOrEmpty(this.txtVolumen.Text))
            {
                m.Volumen = Decimal.Parse(this.txtVolumen.Text);
                m.VolumenMU = Int32.Parse(this.ddlVolumenMU.SelectedValue);
            }
            if (!String.IsNullOrEmpty(this.txtProductionYear.Text)) m.ProductionYear = Int32.Parse(this.txtProductionYear.Text);
            if (!String.IsNullOrEmpty(this.txtDescription.Text)) m.Description = this.txtDescription.Text;
            sucess = MachineController.InsertMachine(m);
            if (sucess)
            {
                this.lblOperationStatus.Text = "Machine sucessfully added";
                this.txtRegistration.Text = "";
                this.txtPower.Text = "";
                this.txtVolumen.Text = "";
                this.txtProductionYear.Text = "";
                this.txtDescription.Text = "";
                this.ddlMachineCategory.SelectedIndex = -1;
                this.ddlPowerMU.SelectedIndex = -1;
                this.ddlVolumenMU.SelectedIndex = -1;

                this.dgvMachines.DataBind();
                LoadMachines();
            }
            else
            {
                this.lblOperationStatus.Text = "Error adding machine";
            }
        }
    }
}
